/**   
 * @文件名称: ProductAction.java
 * @类路径: com.lxj.cms.manage.action.product
 * @描述: TODO
 * @公司：昆明天度网络信息科技有限公司
 * @作者：李兴建
 * @时间：2013-8-14 上午10:42:33
 * @版本：V1.0   
 */

package com.lxj.cms.manage.action.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.lxj.cms.manage.action.ManageBaseAction;
import com.lxj.cms.manage.entity.ProductCategoriesEntity;
import com.lxj.cms.manage.entity.ProductEntity;
import com.lxj.cms.manage.entity.ProductNormalEntity;
import com.lxj.cms.manage.entity.ProductTrashEntity;
import com.lxj.cms.manage.entity.ProductWaitEntity;
import com.lxj.core.action.IBaseAction;
import com.lxj.core.entity.SYSBaseEntity;

/**
 * @类功能说明：
 * @类修改者：
 * @修改日期：
 * @修改说明：
 * @公司：昆明天度网络信息科技有限公司
 * @作者：李兴建
 * @创建时间：2013-8-14 上午10:42:33
 * @版本：V1.0
 */
@SuppressWarnings("serial")
@Results( { @Result(name = "index", location = "/WEB-INF/admin/product/product.jsp"), @Result(name = "userGroup", location = "/WEB-INF/cmsManage/user/userGrop.jsp"), @Result(name = "productPag", location = "/WEB-INF/admin/product/productEdit.jsp") })
public class ProductAction extends ManageBaseAction implements IBaseAction {

	private ProductNormalEntity productNormal;

	private List<ProductCategoriesEntity> categoriesList;

	private Long categoriesList1;
	private Long categoriesList2;

	private Long optionId;

	private Long beanId;
	// 产品图片路径
	private String imageUrl;
	// 产品跳转路径
	private String absoluteUrl;
	// 产品名称
	private String infoTitle;
	// 产品价格
	private String infoPrice;
	// 产品来源
	private String infoSource;

	private String ids;

	// 0:正常产品 1:待审核产品 2：已删除产品
	private int productType;

	// 所属分类
	private String categorie;

	@Override
	public void dataList() {
		Criteria criteria = this.createCriteria(getProductEntity());
		criteria.addOrder(Order.desc("beanLastModidyfiTime"));
		config.setExcludes(new String[] { "beanState", "remark", "brands", "categorie" });
		this.setResult(criteria);
	}

	public String edit() {
		productNormal = null;
		if (null != optionId) {
			try {
				productNormal = this.getBaseService().findById(optionId, ProductNormalEntity.class);
				if (null != productNormal.getCategorie()) {
					ProductCategoriesEntity temp = productNormal.getCategorie();
					if (null != temp.getParent()) {
						categoriesList1 = temp.getParent().getBeanId();
					}
					categoriesList2 = temp.getBeanId();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (null == productNormal) {
			productNormal = new ProductNormalEntity();
		}

		categoriesList = new ArrayList<ProductCategoriesEntity>();
		Criteria criteria = this.createCriteria(ProductCategoriesEntity.class);
		List<ProductCategoriesEntity> pcList = criteria.list();
		for (ProductCategoriesEntity pc : pcList) {
			if (null == pc.getParent()) {
				categoriesList.add(pc);
			}
		}

		return "productPag";
	}

	public void editSave() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {

			ProductCategoriesEntity categories = null;
			if (StringUtils.isNotEmpty(this.getCategorie())) {
				try {
					Long id = Long.parseLong(this.getCategorie());
					categories = this.getBaseService().findById(id, ProductCategoriesEntity.class);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
			if (categories != null) {
				productNormal.setCategorie(categories);
				this.getBaseService().saveOrUpdate(productNormal);
				map.put("success", true);
			} else {
				map.put("failure", true);
				map.put("msg", "分类为必选");
			}

		} catch (Exception e) {
			map.put("failure", true);
			map.put("msg", "保存失败，请重试");
			e.printStackTrace();
		}
		this.setResult(map);
	}

	@Override
	public String editPage() {
		ProductNormalEntity product = this.getBaseService().findById(optionId, ProductNormalEntity.class);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("beanId", product.getBeanId());
		map.put("imageUrl", product.getImageUrl());
		map.put("absoluteUrl", product.getAbsoluteUrl());
		map.put("infoTitle", product.getInfoTitle());
		map.put("infoPrice", product.getInfoPrice());
		map.put("infoSource", product.getInfoSource());
		map.put("categorie", product.getCategorie().getBeanId());
		this.setResult(map, new String[] { "beanState", "remark", "brands" });
		return null;
	}

	@Override
	public String index() {
		// TODO Auto-generated method stub
		return "index";
	}

	public Long getOptionId() {
		return optionId;
	}

	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	public List<ProductCategoriesEntity> getCategoriesList() {
		return categoriesList;
	}

	public void setCategoriesList(List<ProductCategoriesEntity> categoriesList) {
		this.categoriesList = categoriesList;
	}

	public Long getCategoriesList1() {
		return categoriesList1;
	}

	public void setCategoriesList1(Long categoriesList1) {
		this.categoriesList1 = categoriesList1;
	}

	public Long getCategoriesList2() {
		return categoriesList2;
	}

	public void setCategoriesList2(Long categoriesList2) {
		this.categoriesList2 = categoriesList2;
	}

	@Override
	public String preservation() {

		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			ProductNormalEntity product = new ProductNormalEntity();

			product.setBeanId(beanId);
			product.setImageUrl(imageUrl);
			product.setAbsoluteUrl(absoluteUrl);
			product.setInfoTitle(infoTitle);
			product.setInfoPrice(infoPrice);
			product.setInfoSource(infoSource);

			ProductCategoriesEntity categories = null;
			if (StringUtils.isNotEmpty(this.getCategorie())) {
				try {
					Long id = Long.parseLong(this.getCategorie());
					categories = this.getBaseService().findById(id, ProductCategoriesEntity.class);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
			if (categories != null) {
				product.setCategorie(categories);
				this.getBaseService().saveOrUpdate(product);
				map.put("success", true);
			} else {
				map.put("failure", true);
				map.put("msg", "分类为必选");
			}

		} catch (Exception e) {
			map.put("failure", true);
			map.put("msg", "保存失败，请重试");
			e.printStackTrace();
		}
		this.setResult(map);
		return null;
	}

	@Override
	public String remove() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			if (StringUtils.isNotEmpty(this.getIds())) {
				String[] idsTemp = this.getIds().split(",");
				for (String str : idsTemp) {
					if (StringUtils.isEmpty(str)) {
						continue;
					}
					try {
						Long id = Long.parseLong(str);
						this.getBaseService().delete(id, getProductEntity());

					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}

				}
			}
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("failure", true);
			map.put("msg", "删除失败，请重试");
		}
		this.setResult(map);
		return null;
	}

	private Class<? extends SYSBaseEntity> getProductEntity() {
		Class temp = null;
		switch (productType) {
		case 0:
			temp = ProductNormalEntity.class;
			break;
		case 1:
			temp = ProductWaitEntity.class;
			break;
		case 2:
			temp = ProductTrashEntity.class;
			break;

		default:
			break;
		}
		return temp;
	}

	public ProductNormalEntity getProductNormal() {
		return productNormal;
	}

	public void setProductNormal(ProductNormalEntity productNormal) {
		this.productNormal = productNormal;
	}

	public int getProductType() {
		return productType;
	}

	public void setProductType(int productType) {
		this.productType = productType;
	}

	public String getCategorie() {
		return categorie;
	}

	public void setCategorie(String categorie) {
		this.categorie = categorie;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Long getBeanId() {
		return beanId;
	}

	public void setBeanId(Long beanId) {
		this.beanId = beanId;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public String getAbsoluteUrl() {
		return absoluteUrl;
	}

	public void setAbsoluteUrl(String absoluteUrl) {
		this.absoluteUrl = absoluteUrl;
	}

	public String getInfoTitle() {
		return infoTitle;
	}

	public void setInfoTitle(String infoTitle) {
		this.infoTitle = infoTitle;
	}

	public String getInfoPrice() {
		return infoPrice;
	}

	public void setInfoPrice(String infoPrice) {
		this.infoPrice = infoPrice;
	}

	public String getInfoSource() {
		return infoSource;
	}

	public void setInfoSource(String infoSource) {
		this.infoSource = infoSource;
	}

}
